home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / integration / TLSTwistedProtocolWrapper.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.4 KB  |  212 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''TLS Lite + Twisted.'''
  5. from twisted.protocols.policies import ProtocolWrapper, WrappingFactory
  6. from twisted.python.failure import Failure
  7. from AsyncStateMachine import AsyncStateMachine
  8. from gdata.tlslite.TLSConnection import TLSConnection
  9. from gdata.tlslite.errors import *
  10. import socket
  11. import errno
  12.  
  13. class _FakeSocket:
  14.     
  15.     def __init__(self, wrapper):
  16.         self.wrapper = wrapper
  17.         self.data = ''
  18.  
  19.     
  20.     def send(self, data):
  21.         ProtocolWrapper.write(self.wrapper, data)
  22.         return len(data)
  23.  
  24.     
  25.     def recv(self, numBytes):
  26.         if self.data == '':
  27.             raise socket.error, (errno.EWOULDBLOCK, '')
  28.         self.data == ''
  29.         returnData = self.data[:numBytes]
  30.         self.data = self.data[numBytes:]
  31.         return returnData
  32.  
  33.  
  34.  
  35. class TLSTwistedProtocolWrapper(ProtocolWrapper, AsyncStateMachine):
  36.     '''This class can wrap Twisted protocols to add TLS support.
  37.  
  38.     Below is a complete example of using TLS Lite with a Twisted echo
  39.     server.
  40.  
  41.     There are two server implementations below.  Echo is the original
  42.     protocol, which is oblivious to TLS.  Echo1 subclasses Echo and
  43.     negotiates TLS when the client connects.  Echo2 subclasses Echo and
  44.     negotiates TLS when the client sends "STARTTLS"::
  45.  
  46.         from twisted.internet.protocol import Protocol, Factory
  47.         from twisted.internet import reactor
  48.         from twisted.protocols.policies import WrappingFactory
  49.         from twisted.protocols.basic import LineReceiver
  50.         from twisted.python import log
  51.         from twisted.python.failure import Failure
  52.         import sys
  53.         from tlslite.api import *
  54.  
  55.         s = open("./serverX509Cert.pem").read()
  56.         x509 = X509()
  57.         x509.parse(s)
  58.         certChain = X509CertChain([x509])
  59.  
  60.         s = open("./serverX509Key.pem").read()
  61.         privateKey = parsePEMKey(s, private=True)
  62.  
  63.         verifierDB = VerifierDB("verifierDB")
  64.         verifierDB.open()
  65.  
  66.         class Echo(LineReceiver):
  67.             def connectionMade(self):
  68.                 self.transport.write("Welcome to the echo server!\\r\\n")
  69.  
  70.             def lineReceived(self, line):
  71.                 self.transport.write(line + "\\r\\n")
  72.  
  73.         class Echo1(Echo):
  74.             def connectionMade(self):
  75.                 if not self.transport.tlsStarted:
  76.                     self.transport.setServerHandshakeOp(certChain=certChain,
  77.                                                         privateKey=privateKey,
  78.                                                         verifierDB=verifierDB)
  79.                 else:
  80.                     Echo.connectionMade(self)
  81.  
  82.             def connectionLost(self, reason):
  83.                 pass #Handle any TLS exceptions here
  84.  
  85.         class Echo2(Echo):
  86.             def lineReceived(self, data):
  87.                 if data == "STARTTLS":
  88.                     self.transport.setServerHandshakeOp(certChain=certChain,
  89.                                                         privateKey=privateKey,
  90.                                                         verifierDB=verifierDB)
  91.                 else:
  92.                     Echo.lineReceived(self, data)
  93.  
  94.             def connectionLost(self, reason):
  95.                 pass #Handle any TLS exceptions here
  96.  
  97.         factory = Factory()
  98.         factory.protocol = Echo1
  99.         #factory.protocol = Echo2
  100.  
  101.         wrappingFactory = WrappingFactory(factory)
  102.         wrappingFactory.protocol = TLSTwistedProtocolWrapper
  103.  
  104.         log.startLogging(sys.stdout)
  105.         reactor.listenTCP(1079, wrappingFactory)
  106.         reactor.run()
  107.  
  108.     This class works as follows:
  109.  
  110.     Data comes in and is given to the AsyncStateMachine for handling.
  111.     AsyncStateMachine will forward events to this class, and we\'ll
  112.     pass them on to the ProtocolHandler, which will proxy them to the
  113.     wrapped protocol.  The wrapped protocol may then call back into
  114.     this class, and these calls will be proxied into the
  115.     AsyncStateMachine.
  116.  
  117.     The call graph looks like this:
  118.      - self.dataReceived
  119.        - AsyncStateMachine.inReadEvent
  120.          - self.out(Connect|Close|Read)Event
  121.            - ProtocolWrapper.(connectionMade|loseConnection|dataReceived)
  122.              - self.(loseConnection|write|writeSequence)
  123.                - AsyncStateMachine.(setCloseOp|setWriteOp)
  124.     '''
  125.     
  126.     def __init__(self, factory, wrappedProtocol):
  127.         ProtocolWrapper.__init__(self, factory, wrappedProtocol)
  128.         AsyncStateMachine.__init__(self)
  129.         self.fakeSocket = _FakeSocket(self)
  130.         self.tlsConnection = TLSConnection(self.fakeSocket)
  131.         self.tlsStarted = False
  132.         self.connectionLostCalled = False
  133.  
  134.     
  135.     def connectionMade(self):
  136.         
  137.         try:
  138.             ProtocolWrapper.connectionMade(self)
  139.         except TLSError:
  140.             e = None
  141.             self.connectionLost(Failure(e))
  142.             ProtocolWrapper.loseConnection(self)
  143.  
  144.  
  145.     
  146.     def dataReceived(self, data):
  147.         
  148.         try:
  149.             if not self.tlsStarted:
  150.                 ProtocolWrapper.dataReceived(self, data)
  151.             else:
  152.                 self.fakeSocket.data += data
  153.                 while self.fakeSocket.data:
  154.                     AsyncStateMachine.inReadEvent(self)
  155.                     continue
  156.                     self.fakeSocket
  157.         except TLSError:
  158.             e = None
  159.             self.connectionLost(Failure(e))
  160.             ProtocolWrapper.loseConnection(self)
  161.  
  162.  
  163.     
  164.     def connectionLost(self, reason):
  165.         if not self.connectionLostCalled:
  166.             ProtocolWrapper.connectionLost(self, reason)
  167.             self.connectionLostCalled = True
  168.         
  169.  
  170.     
  171.     def outConnectEvent(self):
  172.         ProtocolWrapper.connectionMade(self)
  173.  
  174.     
  175.     def outCloseEvent(self):
  176.         ProtocolWrapper.loseConnection(self)
  177.  
  178.     
  179.     def outReadEvent(self, data):
  180.         if data == '':
  181.             ProtocolWrapper.loseConnection(self)
  182.         else:
  183.             ProtocolWrapper.dataReceived(self, data)
  184.  
  185.     
  186.     def setServerHandshakeOp(self, **args):
  187.         self.tlsStarted = True
  188.         AsyncStateMachine.setServerHandshakeOp(self, **args)
  189.  
  190.     
  191.     def loseConnection(self):
  192.         if not self.tlsStarted:
  193.             ProtocolWrapper.loseConnection(self)
  194.         else:
  195.             AsyncStateMachine.setCloseOp(self)
  196.  
  197.     
  198.     def write(self, data):
  199.         if not self.tlsStarted:
  200.             ProtocolWrapper.write(self, data)
  201.         else:
  202.             AsyncStateMachine.setWriteOp(self, data)
  203.  
  204.     
  205.     def writeSequence(self, seq):
  206.         if not self.tlsStarted:
  207.             ProtocolWrapper.writeSequence(self, seq)
  208.         else:
  209.             AsyncStateMachine.setWriteOp(self, ''.join(seq))
  210.  
  211.  
  212.